package com.xmcc.service.impl;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.xmcc.common.PageCommon;
import com.xmcc.dao.SysLogMapper;
import com.xmcc.dto.SysLogDto;
import com.xmcc.exception.ParamException;
import com.xmcc.pojo.SysLog;
import com.xmcc.pojo.SysLogWithBLOBs;
import com.xmcc.pojo.SysRole;
import com.xmcc.pojo.SysUser;
import com.xmcc.service.SysAclService;
import com.xmcc.service.SysLogService;
import com.xmcc.service.SysRoleService;
import com.xmcc.service.SysUserService;
import com.xmcc.utill.Constant;
import com.xmcc.utill.IPUtils;
import com.xmcc.utill.JsonUtil;
import com.xmcc.utill.ThreadLocalCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogMapper sysLogMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService sysRoleService;
    @Override
    public void saveLogUser(SysUser oldUser, SysUser newUser) {
        //日志对象属性：目标id：
        Integer targetId = null;
        if (oldUser==null){ //添加新用户，旧用户对象就是为空的
            targetId = newUser.getId();
        }else { // 删除用户，或者修改用户
            targetId = oldUser.getId();
        }
        //日志对象属性：操作旧新用户对象字符串数据：
        String oldValue = oldUser==null?"":JsonUtil.object2string(oldUser);
        String newValue = newUser==null?"":JsonUtil.object2string(newUser);

        SysLogWithBLOBs sysLogWithBLOBs = new SysLogWithBLOBs();

        sysLogWithBLOBs.setTargetId(targetId);
        sysLogWithBLOBs.setType(Constant.LOG_USER_TYPE);
        sysLogWithBLOBs.setOldValue(oldValue);
        sysLogWithBLOBs.setNewValue(newValue);
        sysLogWithBLOBs.setOperator(ThreadLocalCommon.popSysUser().getUsername());
        sysLogWithBLOBs.setOperateTime(new Date());
        sysLogWithBLOBs.setOperateIp(IPUtils.getIpAddress(ThreadLocalCommon.popHttpServletRequest()));
        //日志状态：当前是否复原过，0：没有，1：复原过
        sysLogWithBLOBs.setStatus(1);

        sysLogMapper.insertSelective(sysLogWithBLOBs);
    }

    @Override
    public void saveRoleAcl(Integer roleId, List<Integer> oldAclIds, List<Integer> newAclIds) {
        Preconditions.checkNotNull(roleId,"角色不存在");
        StringBuffer oldValue=new StringBuffer();
        if(!CollectionUtils.isEmpty(oldAclIds)){//以前的不为空
            for (Integer oldAclId : oldAclIds) {
                oldValue.append(oldAclId+",");
            }
        }
        StringBuffer newValue = new StringBuffer();
        if(!CollectionUtils.isEmpty(newAclIds)){
            for(Integer newAclId:newAclIds)
             newValue.append(newAclId+",");
        }
        SysLogWithBLOBs sysLogWithBLOBs = new SysLogWithBLOBs();

        sysLogWithBLOBs.setTargetId(roleId);
        sysLogWithBLOBs.setType(Constant.LOG_ROLE_ACL);
        sysLogWithBLOBs.setOldValue(oldValue.toString());
        sysLogWithBLOBs.setNewValue(newValue.toString());
        sysLogWithBLOBs.setOperator(ThreadLocalCommon.popSysUser().getUsername());
        sysLogWithBLOBs.setOperateTime(new Date());
        sysLogWithBLOBs.setOperateIp(IPUtils.getIpAddress(ThreadLocalCommon.popHttpServletRequest()));
        //日志状态：当前是否复原过，0：没有，1：复原过
        sysLogWithBLOBs.setStatus(1);

        sysLogMapper.insertSelective(sysLogWithBLOBs);
    }

    @Override
    public PageCommon<SysLogWithBLOBs> queryList(Integer pageSize, Integer pageNo, SysLogDto sysLogDto) {
        pageSize = pageSize ==null||pageSize<5?5:pageSize;
        pageNo = pageNo ==null||pageNo<1?1:pageNo;
        //查询总条数
        Integer begin = (pageNo-1)*pageSize;
        long totalCount = sysLogMapper.queryPageCount(sysLogDto);
        // 查询数据
        List<SysLogWithBLOBs> logList = sysLogMapper.queryPageList(sysLogDto,begin,pageSize);

        PageCommon<SysLogWithBLOBs> pageCommon = new PageCommon<>();
        pageCommon.setPageSize(pageSize);
        pageCommon.setCurPage(pageNo);
        pageCommon.setTotalCount(totalCount);
        pageCommon.setPageList(logList);
        return pageCommon;
    }

    @Override
    public void recover(Integer id) {
        //通过id查询出这条记录
        SysLogWithBLOBs sysLogWithBLOBs = sysLogMapper.selectByPrimaryKey(id);
        Preconditions.checkNotNull(sysLogWithBLOBs,"待还原的记录不存在");
        if(sysLogWithBLOBs.getStatus()!=0){
            throw  new ParamException("该记录已经还原过");
        }
    //1：部门，3：用户，2：权限模块，4：权限，5：角色，6：角色用户关系，7：角色权限关系
        switch (sysLogWithBLOBs.getType()){
            case 1:
            case 2:
            case 3://
                sysUserService.recover(sysLogWithBLOBs);
                break;
            case 4:
            case 5:
            case 6:
            case 7:
                sysRoleService.recover(sysLogWithBLOBs);
        }
       String newValue =  sysLogWithBLOBs.getNewValue();
        sysLogWithBLOBs.setStatus(1);
        sysLogWithBLOBs.setNewValue(sysLogWithBLOBs.getOldValue());
        sysLogWithBLOBs.setOldValue(newValue);
        sysLogWithBLOBs.setOperator(ThreadLocalCommon.popSysUser().getUsername());
        sysLogWithBLOBs.setOperateTime(new Date());
        sysLogWithBLOBs.setOperateIp(IPUtils.getIpAddress(ThreadLocalCommon.popHttpServletRequest()));
        sysLogWithBLOBs.setId(null);
        sysLogMapper.insertSelective(sysLogWithBLOBs);
    }


}
